import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class ChannelSampleHomePage extends StatefulWidget {
  ChannelSampleHomePage({Key? key}) : super(key: key);

  @override
  _ChannelSampleHomePageState createState() => _ChannelSampleHomePageState();
}

class _ChannelSampleHomePageState extends State<ChannelSampleHomePage> {

  static const platform = const MethodChannel('samples.flutter.io/battery');

  @override
  void initState() {
    _getBatteryLevel();
    super.initState();
  }


  // Get battery level.
  String _batteryLevel = 'Unknown battery level.';
  _getBatteryLevel() async {
    print("_getBatteryLevel | called");
    String batteryLevel;
    try {
      final int result = await platform.invokeMethod('getBatteryLevel');
      batteryLevel = 'Battery level at $result % .';
    } on PlatformException catch (e) {
      batteryLevel = "Failed to get battery level: '${e.message}'.";
    }

    setState(() {
      _batteryLevel = batteryLevel;
    });
  }


  @override
  Widget build(BuildContext context) {


    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text("Channel Sample"),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Text("当前电量:$_batteryLevel"),
      ),
    );
  }


}
